/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-18     lfz         first version
 */
#include <elog.h>
#include <elog_flash.h>
#include <rthw.h>
#include <rtthread.h>
#include <sys/time.h>

static struct rt_semaphore output_lock;

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
static struct rt_semaphore output_notice;
static void async_output(void *arg);
#endif

/**
 * EasyLogger port initialize
 *
 * @return result
 */
ElogErrCode elog_port_init(void) 
{
    ElogErrCode result = ELOG_NO_ERR;

    /* add your code here */
	rt_sem_init(&output_lock, "e_lock", 1, RT_IPC_FLAG_PRIO);
    
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
    rt_thread_t async_thread = NULL; 
    rt_sem_init(&output_notice, "e_async", 0, RT_IPC_FLAG_PRIO);
    async_thread = rt_thread_create("elog", 
									async_output, 
									NULL, 
									1024, 
									RT_THREAD_PRIORITY_MAX - 3, 
									10);
    if (async_thread) {
        rt_thread_startup(async_thread);
    }
#endif
    return result;
}

/**
 * EasyLogger port deinitialize
 *
 */
void elog_port_deinit(void) 
{

    /* add your code here */

}

/**
 * output log port interface
 *
 * @param log output of log
 * @param size log size
 */
void elog_port_output(const char *log, size_t size) 
{  
	/* output to terminal */
    rt_kprintf("%.*s", size, log);
    /* output to flash */
#ifdef PKG_EASYFLASH_LOG
    elog_flash_write(log, size);
#endif
}

/**
 * output lock
 */
void elog_port_output_lock(void) 
{ 
    rt_sem_take(&output_lock, RT_WAITING_FOREVER);
}

/**
 * output unlock
 */
void elog_port_output_unlock(void) 
{ 
    rt_sem_release(&output_lock);
}

/**
 * get current time interface
 *
 * @return current time
 */
const char *elog_port_get_time(void) 
{
#define CUR_SYSTEM_TIME_BUFF (20)
	static char cur_system_time[CUR_SYSTEM_TIME_BUFF] = { 0 };
    
#ifdef RT_USING_RTC
    {
    time_t now;
    struct tm gmt = {0};
    /* output current time */
    now = time(RT_NULL);
    localtime_r(&now, &gmt);
    
    rt_snprintf(cur_system_time, CUR_SYSTEM_TIME_BUFF, 
                "%02d-%02d %02d:%02d:%02d",
                gmt.tm_mon + 1, 
                gmt.tm_mday,
                gmt.tm_hour, 
                gmt.tm_min, 
                gmt.tm_sec); 
    }
#else
    rt_snprintf(cur_system_time, CUR_SYSTEM_TIME_BUFF, "%010d", rt_tick_get());
#endif
    return cur_system_time;
}

/**
 * get current process name interface
 *
 * @return current process name
 */
const char *elog_port_get_p_info(void) 
{ 
    return "";
}

/**
 * get current thread name interface
 *
 * @return current thread name
 */
const char *elog_port_get_t_info(void) 
{ 
    /* add your code here */
    return rt_thread_self()->name;
}

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
void elog_async_output_notice(void) 
{
    rt_sem_release(&output_notice);
}

static void async_output(void *arg) 
{
    size_t get_log_size = 0;
    static char poll_get_buf[ELOG_LINE_BUF_SIZE - 4];

    while(true) {
        /* waiting log */
        rt_sem_take(&output_notice, RT_WAITING_FOREVER);
        /* polling gets and outputs the log */
        while(true) {
#ifdef ELOG_ASYNC_LINE_OUTPUT
            get_log_size = elog_async_get_line_log(poll_get_buf, sizeof(poll_get_buf));
#else
            get_log_size = elog_async_get_log(poll_get_buf, sizeof(poll_get_buf));
#endif
            if (get_log_size) {
                elog_port_output(poll_get_buf, get_log_size);
            } else {
                break;
            }
        }
    }
}
#endif

